package com.cxm.homework8.jdbcTmp.query;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;

public class UserDaoImpl implements UserDao {
    JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }

    @Override
    public User finUserById(int id) {
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        String sql = "select * from user where id =?";
        User user = jdbcTemplate.queryForObject(sql, rowMapper, id);
        return user;
    }

    @Override
    public List<User> findUserList() {
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        String sql = "select * from user";
        List<User> users = jdbcTemplate.query(sql, rowMapper);
        return users;
    }
}
